// pages/msg_yield_add/msg_yield_add.js
let app = getApp();


Page({

    /**
     * 页面的初始数据
     */
    data: {
        array1: [{
                'LCODE':'01',
                'LNAME': '一班'
            },
            {
                'LCODE':'02',
                'LNAME': '二班'
            },
            {
                'LCODE':'03',
                'LNAME': '三班'
            },
            {
                'LCODE':'04',
                'LNAME': '四班'
            },
            {
                'LCODE':'05',
                'LNAME': '五班'
            },
        ],
        table: [],
        my_table: [],
        imgList: [],
        LCURRENCYID: "人民币",
        LUNIT: '千克',
        LREMARK: '',
        LBANCI:'',
    },
    bindPickerChange1(e) { // 车间
        this.setData({
            index1: e.detail.value,
        })
        for (var i = 0; i < this.data.array1.length; i++) {
            if (i == e.detail.value) {
                this.setData({
                    LBANCI: this.data.array1[i].LNAME
                })
            }
        }
    },
    // ListTouch触摸开始
    ListTouchStart(e) {
        this.setData({
            ListTouchStart: e.touches[0].pageX
        })
    },
    // ListTouch计算方向
    ListTouchMove(e) {
        this.setData({
            ListTouchDirection: e.touches[0].pageX - this.data.ListTouchStart > 0 ? 'right' : 'left'
        })
    },
    // ListTouch计算滚动
    ListTouchEnd(e) {
        if (this.data.ListTouchDirection == 'left') {
            this.setData({
                modalName: e.currentTarget.dataset.target
            })
        } else {
            this.setData({
                modalName: null
            })
        }
        this.setData({
            ListTouchDirection: null
        })
    },
    empty() { //清空数据
        wx.redirectTo({
            url: '../msg_yield_add/msg_yield_add'
        })
    },
    yield_add() { // 添加产量录入明细信息
        wx.navigateTo({
            url: '../msg_yield_add_details/msg_yield_add_details?list=' + '1'
        })
    },
    bindinputip(e) {
        if (e.currentTarget.dataset.id == '1') {
            this.data.LUNIT = e.detail.value
        } else if (e.currentTarget.dataset.id == '2') {
            this.data.LCURRENCYID = e.detail.value
        } else if (e.currentTarget.dataset.id == '3') {
            this.data.LREMARK = e.detail.value
        } 
    },

    ChooseImage() {
        wx.chooseImage({
            count: 1, //默认9
            sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
            sourceType: ['album'], //从相册选择
            success: (res) => {
                if (this.data.imgList.length != 0) {
                    this.setData({
                        imgList: this.data.imgList.concat(res.tempFilePaths)
                    })
                } else {
                    this.setData({
                        imgList: res.tempFilePaths
                    })
                }
            }
        });
    },
    //扫码按钮
    bindsm: function (e) {
        var that = this
        wx.scanCode({
            onlyFromCamera: true,
            // scanType: 'qrCode',
            success(res) {
                console.log('扫码成功', res.result)
                let result = res.result
                wx.request({
                    url: 'https://' + app.globalData.url + '/Contract/findContractBYZHULID',
                    data: {
                        LID: result
                    },
                    method: 'GET',
                    dataType: 'json',
                    responseType: 'text',
                    header: {
                        'content-type': 'application/json;charset=utf-8'
                    },
                    // 访问成功
                    success: function (res) {
                        if (res.data.success == true) {
                            that.setData({
                                table: res.data.obj
                            })

                        } else {
                            wx.showModal({
                                title: '提示',
                                content: '获取数据失败',
                                showCancel: false,
                                confirmText: '确定',
                                success(res) {
                                    if (res.confirm) {

                                    }
                                }
                            })
                        }
                    },
                    fail: function (res) {
                        wx.showModal({
                            title: '提示',
                            content: '访问服务器失败！',
                            showCancel: false,
                            confirmText: '确定',
                            success(res) {
                                if (res.confirm) {
                                    console.log('用户点击确定')

                                }
                            }
                        })
                    },
                })
            },
            fail(res) {
                wx.showModal({
                    title: '提示',
                    content: '扫描二维码失败!',
                    showCancel: false,
                    confirmText: '确定',
                    success(res) {
                        if (res.confirm) {

                        }
                    }
                })
            }
        })

    },
    scan(e) {
        let that = this
        if (that.data.LBANCI == '') {
            wx.showModal({
                title: '提示',
                content: '请选择班次',
                showCancel: false,
                confirmText: '确定',
                success(res) {
                    if (res.confirm) {

                    }
                }
            })
        } else{
            if (that.data.table != '') {
                let table = [{
                    'LID': that.data.table[0].LID,
                    'LTICKETNO': that.data.table[0].LTICKETNO, //编号
                    "LTICKETDATE": that.data.dateright, //日期
                    "LPTNCODE": that.data.table[0].LPTNCODE, //客户
                    "LCOMPANYID": that.data.table[0].LCOMPANYID, //签订公司
                    "LSALEOPID": that.data.table[0].LSALEOPID, //业务员
                    'LITEMCODE': that.data.table[0].LITEMCODE, // 产品编号
                    'LITEMNAME': that.data.table[0].LITEMNAME, //产品名称
                    'LITEMSTD': that.data.table[0].LITEMSTD, // 规格
                    'LITEMCOMPOS': that.data.table[0].LITEMCOMPOS, // 成份
                    "LUNIT": that.data.LUNIT, //计量方式
                    'LBANCI': that.data.LBANCI, // 班次
                    'LCURRENCYID': that.data.LCURRENCYID, // 币种
                    "LMAKEOPID": app.globalData.lusercode, // 制单人
                    "LMAKEDATE": that.data.dateright, //制单日期
                    "LREMARK": that.data.LREMARK, //备注
                }]
                let bb = {
                    'table': table,
                    "dts": that.data.my_table
                }
                wx.request({
                    url: 'https://' + app.globalData.url + '/SaleOrder/addSaleOrderAll',
                    data: {
                        "model": JSON.stringify(bb),
                    },
                    method: 'POST',
                    dataType: 'json',
                    responseType: 'text',
                    header: {
                        'content-type': 'application/x-www-form-urlencoded'
                    },
                    // 访问成功
                    success: function (res) {
                        if (res.data.success == true) {
                            wx.navigateBack({
                                delta: 1
                            })
                            wx.showToast({
                                title: '成功',
                                icon: 'success',
                                duration: 2000
                            })
                        } else {
                            wx.showModal({
                                title: '提示',
                                content: '保存失败',
                                showCancel: false,
                                confirmText: '确定',
                                success(res) {
                                    if (res.confirm) {

                                    }
                                }
                            })
                        }
                    },
                    fail: function (res) {
                        wx.showModal({
                            title: '提示',
                            content: '访问服务器失败 ',
                            showCancel: false,
                            confirmText: '确定',
                            success(res) {
                                if (res.confirm) {

                                }
                            }
                        })
                    },
                })
            } else {
                wx.showModal({
                    title: '提示',
                    content: '请扫描生产通知单',
                    showCancel: false,
                    confirmText: '确定',
                    success(res) {
                        if (res.confirm) {

                        }
                    }
                })
            }
        }


    },
    ckantable(e){
        wx.navigateTo({
            url: '../msg_yield_add_details/msg_yield_add_details?list=' + '3' + '&index=' + e.currentTarget.dataset.index
        })
    },
    shanchu(e){
        let list = this.data.my_table
        console.log(e.currentTarget.dataset.index)
        list.splice(e.currentTarget.dataset.index, 1)
        app.globalData.newtable.splice(e.currentTarget.dataset.index, 1)
        this.setData({
            my_table: list,
        })
        wx.showToast({
            title: '成功',
            icon: 'success',
            duration: 2000
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        let that = this
        app.globalData.my_table = ''
        var date = new Date()
        var year = date.getFullYear()
        var month = date.getMonth() + 1
        var day = date.getDate()
        var lw = new Date(date - 1000 * 60 * 60 * 24 * 30) //最后一个数字30可改，30天的意思

        var date1 = new Date();
        var date3 = new Date(date1);
        date3.setDate(date1.getDate() + 30);
        var enddate = (date3.getFullYear() + "-" + ((date3.getMonth() + 1) < 10 ? '0' + (date3.getMonth() + 1) : (date3.getMonth() + 1)) + "-" + (date3.getDate() < 10 ? '0' + date3.getDate() : date3.getDate()))
        var lastY = lw.getFullYear()
        var lastM = lw.getMonth() + 1
        var lastD = lw.getDate()
        var startdate = lastY + '-' + (lastM < 10 ? '0' + lastM : lastM) + '-' + (lastD < 10 ? '0' + lastD : lastD) //三十天之前日期
        if (month < 10) {
            month = '0' + month
        }
        if (day < 10) {
            day = '0' + day
        }
        var dateright = year + '-' + month + '-' + day

        that.setData({
            dateright: dateright,
            LMAKEOPID: app.globalData.lusername
        })
        that.setData({
            dateleft: startdate
        })
        wx.request({
            url: 'https://' + app.globalData.url + '/machineInfo/getCode',
            data: {
                RuleId: 113
            },
            method: 'GET',
            dataType: 'json',
            responseType: 'text',
            header: {
                'content-type': 'application/json;charset=utf-8'
            },
            success: function (res) {
                if (res.data.success == true) {
                    that.setData({
                        LTICKETNO: res.data.obj
                    })
                } else {
                    wx.showModal({
                        title: '提示',
                        content: '获取数据失败',
                        showCancel: false,
                        confirmText: '确定',
                        success(res) {
                            if (res.confirm) {

                            }
                        }
                    })
                }
            },
            fail: function (res) {
                that.setData({
                    loadModal: false
                })
                wx.showModal({
                    title: '提示',
                    content: '获取单号失败',
                    showCancel: false,
                    confirmText: '确定',
                    success(res) {
                        if (res.confirm) {

                        }
                    }
                })
            },
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        let that = this
        if (app.globalData.my_table != '') {
            console.log(app.globalData.my_table.LPHOTO)
            app.globalData.my_table.LPHOTO = "https://" + app.globalData.url + "/image/UserImage/" + app.globalData.my_table.LPHOTO
            that.data.my_table.push(app.globalData.my_table)
            that.setData({
                my_table: that.data.my_table
            })
            app.globalData.my_table = ''
            console.log(that.data.my_table)
            console.log(app.globalData.newtable)
        }
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})